﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête add_comment d'une photo</summary>
    /// <see cref="upload"></see>
    [DataContract]
    public class add_comment
    {
        /// <summary>statuts de l'envoie du commentaire sur la photo = 'ok'</summary>
        [DataMember]
        public string status { get; set; }        
    }


    public delegate void AddCommentEventHandler(object sender, AddCommentEventArgs e);
    public class AddCommentEventArgs : EventArgs
    {
        public AddCommentEventArgs(add_comment ac)
        {
            _result = ac;
        }

        private add_comment _result;
        public add_comment result { get { return _result; } }
    }


    public partial class apitxt : api
    {

        public event AddCommentEventHandler AddCommentCompleted;

        /// <summary>Permet de commenter une photo</summary>
        /// <c>//https://apitxt.hellotipi.com/photo/add_comment/?album_id=00000&photo_id=000000&text=commentaire</c>
        /// <param name="site">Site de famille dont la liste des albums est à recupérer</param>
        /// <returns>La liste des albums du site de famille <paramref name="site"/></returns>
        public void add_comment(int photo_id, string text, int album_id = 0)
        {

            base.APICallCompleted += new APICallCompletedEventHandler(AddCommentCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("photo_id", photo_id.ToString()),
                                 new KeyValuePair<string, string>("text", text),
                             };

            if (album_id > 0)
                querystring.Add(new KeyValuePair<string, string>("album_id", album_id.ToString()));

            GetUrlApi("photo/add_comment.json", querystring);

        }

        void AddCommentCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                AddCommentCompleted.Invoke(sender, new AddCommentEventArgs(serializeTools.Deserialize<add_comment>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(AddCommentCallCompleted);
            }
        }

    }
}
